package com.tianhui.consignortwofei.other.auth;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author fgs
 * @date 2021/5/14
 * @description
 **/
public class CheckCameraPermission {
    private Context context;
    private RxPermissions rxPermissions;

    public CheckCameraPermission(Context context) {
        this.context = context;
        rxPermissions = new RxPermissions((FragmentActivity) context);
    }

    public boolean checkPermission() {

        if (Build.VERSION.SDK_INT > 23) {
            AtomicBoolean isGranted = new AtomicBoolean(false);
            rxPermissions.requestEach(Manifest.permission.CAMERA)
                    .subscribe(permission -> {
                        if (permission.granted) {
                            isGranted.set(true);
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            isGranted.set(false);
                        } else {
                            showPermissionTipDialog(context);
                            isGranted.set(false);
                        }
                    });
            return isGranted.get();
        } else {
            if (cameraIsCanUse()) {
                return true;
            } else {
                showPermissionTipDialog(context);
                return false;
            }

        }
    }

    private void showPermissionTipDialog(Context context) {
        new MaterialDialog.Builder(context)
                .content("该功能需要相机权限，是否开启?")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent intent = new Intent();
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        if (Build.VERSION.SDK_INT >= 9) {
                            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                        } else if (Build.VERSION.SDK_INT <= 8) {
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                            intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
                        }
                        try {
                            context.startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                })
                .show();
    }

    /**
     * 6.0以下相机权限检测
     * 返回true 表示可以使用 返回false表示不可以使用
     */
    private boolean cameraIsCanUse() {
        boolean isCanUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            Camera.Parameters mParameters = mCamera.getParameters(); //针对魅族手机
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            isCanUse = false;
        }
        if (mCamera != null) {
            try {
                mCamera.release();
            } catch (Exception e) {
                e.printStackTrace();
                return isCanUse;
            }
        }
        return isCanUse;
    }
}
